package com.mobile.manager.controller;

import com.mobile.manager.pojo.Device;
import com.mobile.manager.service.IDeviceService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.List;

/**
 * Created by zhangdi on 16/4/24.
 */

@Controller
@RequestMapping("/device")
public class DeviceController {
    @Resource
    private IDeviceService deviceService;
    @RequestMapping("/showDevice")
    public String getDeviceById(HttpServletRequest request,Model model){
            int deviceId = Integer.parseInt(request.getParameter("id"));
            Device device = this.deviceService.getDeviceById(deviceId);
            model.addAttribute("device", device);
            return "showDevice";

    }
    //展示设备列表
    @RequestMapping("/showAllDevice")
    public String getAllDevice(HttpServletRequest request,Model model){
        //int deviceId = Integer.parseInt(request.getParameter("id"));
        List<Device> devices = this.deviceService.getAllDevice();
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy年MM月dd日");//小写的mm表示的是分钟
        for(Device device :devices){
            String date = sdf.format(device.getBuy_time());
            device.setDate(date);
        }
        model.addAttribute("devices", devices);
        return "showAllDevice";

    }

    //查询设备列表
    @RequestMapping(value = "/searchDevice",produces = "text/html;charset=UTF-8")
    public String searchDevice(HttpServletRequest request,Model model) throws UnsupportedEncodingException {
        request.setCharacterEncoding("UTF-8");

        Device device = new Device();
        String search_group="all";
        String search_os="all";
        String search_name="";
        String search_owner="";
        String search_os_version="";
        String search_resolution="";
        if(request.getParameter("search_os")!=null){search_os = request.getParameter("search_os");}
        if(request.getParameter("search_group")!=null){
        search_group = request.getParameter("search_group");}
        if(request.getParameter("search_name")!=null){
            search_name = java.net.URLDecoder.decode(request.getParameter("search_name"), "utf-8");
        }
        if(request.getParameter("search_owner")!=null){
            search_owner = java.net.URLDecoder.decode(request.getParameter("search_owner"),"utf-8");

        }
        if(request.getParameter("search_os_version")!=null){
            search_os_version = java.net.URLDecoder.decode(request.getParameter("search_os_version"),"utf-8");

        }
        if(request.getParameter("search_resolution")!=null){
            search_resolution = java.net.URLDecoder.decode(request.getParameter("search_resolution"),"utf-8");
        }

        System.out.println("设备名称是"+search_name);
        device.setOs(search_os);
        device.setGroup_info(search_group);
        device.setDevice_name(search_name);
        device.setOwner(search_owner);
        device.setOs_version(search_os_version);
        device.setResolution(search_resolution);
        List<Device> devices = this.deviceService.searchDevice(device);
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy年MM月dd日");//小写的mm表示的是分钟
        SimpleDateFormat sdf1=new SimpleDateFormat("yyyy年MM月dd日");
        for(Device device1 :devices){
            if(device1.getBorrower()==null){
                device1.setBorrower("");
            }
            String date = sdf.format(device1.getBuy_time());
            if(device1.getGiveback_time() ==null){
                device1.setGiveback_time(device1.getBuy_time());
            }
            if(device1.getGiveback_time() !=null){
            String backdate = sdf1.format(device1.getGiveback_time());
                device1.setBackdate(backdate);
            }
            device1.setDate(date);

        }
        model.addAttribute("devices", devices);
        return "showAllDevice";

    }
    @ResponseBody
    @RequestMapping("/showAllDeviceJson")
    public List<Device> getAllDeviceJson(HttpServletRequest request,Model model){
        //int deviceId = Integer.parseInt(request.getParameter("id"));
        List<Device> devices = this.deviceService.getAllDevice();
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy年MM月dd日");//小写的mm表示的是分钟
        for(Device device :devices){
            String date = sdf.format(device.getBuy_time());
            device.setDate(date);
        }
        //model.addAttribute("devices", devices);
        return devices;

    }

    //查询设备列表
    @ResponseBody
    @RequestMapping("/searchDeviceJson")
    public List<Device> searchDeviceJson(HttpServletRequest request,Model model) throws UnsupportedEncodingException {
        request.setCharacterEncoding("UTF-8");
        Device device = new Device();
        String search_os = request.getParameter("search_os");
        String search_group = request.getParameter("search_group");
        String search_name = request.getParameter("search_name");
        String search_owner = request.getParameter("search_owner");
        device.setOs(search_os);
        device.setGroup_info(search_group);
        device.setDevice_name(search_name);
        device.setOwner(search_owner);
        List<Device> devices = this.deviceService.searchDevice(device);
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy年MM月dd日");//小写的mm表示的是分钟
        for(Device device1 :devices){
            String date = sdf.format(device1.getBuy_time());
            device1.setDate(date);
        }
        //model.addAttribute("devices", devices);
        return devices;

    }


    @RequestMapping("/getDeviceDetail")
    public String getDeviceDetail(HttpServletRequest request,Model model){
        int id = Integer.parseInt(request.getParameter("deviceId"));
        Device device = new Device();
        device = this.deviceService.getDeviceById(id);
        model.addAttribute("device", device);
        return "deviceDetail";
    }

}
